package com.lfs.admin.controller.channel;

import com.lfs.admin.model.entity.channel.ChannelGroupEntity;
import com.lfs.admin.model.entity.channel.ChannelToGroupEntity;
import com.lfs.admin.model.vo.channel.ChannelGroupVO;
import com.lfs.admin.model.vo.channel.ChannelToGroupVO;
import com.lfs.admin.service.ChannelGroupService;
import com.lfs.base.dto.Result;
import com.lfs.base.dto.ResultObject;
import com.lfs.base.exception.BusinessException;
import com.lfs.base.util.CollectionUtils;
import com.lfs.base.util.StringUtils;
import com.lfs.common.annotation.Log;
import com.lfs.common.core.controller.BaseController;
import com.lfs.common.core.domain.AjaxResult;
import com.lfs.common.core.page.TableDataInfo;
import com.lfs.common.enums.BusinessType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.nio.channels.Channel;
import java.util.List;

@RestController
@RequestMapping("/channelGroup")
public class ChannelGroupController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ChannelGroupController.class);

    @Autowired
    private ChannelGroupService channelGroupService;

    /**
     * 查询通道组列表接口
     * @param channelGroupVO
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:query')")
    @PostMapping("/queryChannelGroupList")
    public TableDataInfo queryChannelGroupList(ChannelGroupVO channelGroupVO) {

        if(null == channelGroupVO){
            throw new BusinessException("查询通道组列表接口参数不能为空!");
        }

        logger.info("根据{},查询通道组列表请求参数", channelGroupVO.toString());
        startPage();
        List<ChannelGroupEntity> productList = channelGroupService.queryChannelGroupList(channelGroupVO);
        return getDataTable(productList);
    }

    /**
     * 查询通道组信息
     * @param agentInfoVO
     * @return
     * @throws Exception
     */
    /**
     * 根据产品组编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:query')")
    @GetMapping(value = { "/getChannelGroup/{id}"})
    public AjaxResult getChannelGroupInfo(@PathVariable(value = "id", required = false) Integer id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, channelGroupService.getChannelGroupById(id));
        }
        return ajax;
    }

    /**
     * 查询通道组组下通道列表接口
     * @param channelToGroupVO
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:query')")
    @PostMapping("/getChannelToGroup")
    public TableDataInfo getChannelToGroup(ChannelToGroupVO channelToGroupVO) {
        logger.info("根据{},查询通道组下通道列表请求参数", channelToGroupVO.toString());
        startPage();
        List<ChannelToGroupEntity> channelToGroupList = channelGroupService.getChannelToGroup(channelToGroupVO);
        return getDataTable(channelToGroupList);
    }

    /**
     * 更新通道组信息
     * @param channelGroupVO
     * @return
     */
    @PutMapping("/updateChannelGroup")
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:update')")
    @Log(title = "通道组管理更新", businessType = BusinessType.UPDATE)
    public Result<?> updateChannelGroup(@RequestBody ChannelGroupVO channelGroupVO){
        if(null == channelGroupVO.getId()){
            throw new BusinessException("请求更新的编码不能为空!");
        }
        int result = channelGroupService.updateChannelGroup(channelGroupVO);
        if(result <= 0){
            logger.info("未更新到任意一条记录!");
        }
        return  ResultObject.successMessage("更新成功!");
    }

    /**
     * 更新通道组信息
     * @param channelToGroupVO
     * @return
     */
    @PutMapping("/updateWeight")
    @Log(title = "产品组管理", businessType = BusinessType.UPDATE)
    public AjaxResult updateWeight(@RequestBody ChannelToGroupVO channelToGroupVO){
        if(null == channelToGroupVO.getChannelNum()){
            throw new BusinessException("请求更新的通道编码不能为空!");
        }
        int result = channelGroupService.updateWeight(channelToGroupVO);
        if(result <= 0){
            logger.info("未更新到任意一条记录!");
        }
        return  toAjax(result);
    }

    /**
     * 新增通道组信息
     * @param channelGroupVO
     * @return
     */
    @PostMapping("/addChannelGroup")
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:add')")
    @Log(title = "通道组管理", businessType = BusinessType.INSERT)
    public Result<?> addChannelGroup(@RequestBody ChannelGroupVO channelGroupVO){
        if(null == channelGroupVO.getChannelGroupName()){
            throw new BusinessException("通道组名称不能为空!");
        }
        int result = channelGroupService.addChannelGroup(channelGroupVO);
        if(result <= 0){
            throw new BusinessException("新增通道组失败!");
        }
        return  ResultObject.successMessage("新增通道组成功!");
    }

    /**
     * 通道组新增通道信息
     * @param channelGroupVO
     * @return
     */
    @PostMapping("/addChannelToGroup")
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:add')")
    @Log(title = "通道组添加通道管理", businessType = BusinessType.INSERT)
    public Result<?> addChannelToGroup(@RequestBody ChannelGroupVO channelGroupVO){
        if(null == channelGroupVO.getId()){
            throw new BusinessException("通道组编码不能为空!");
        }
        if(CollectionUtils.isEmpty(channelGroupVO.getChannelNums())){
            throw new BusinessException("通道编码不能为空!");
        }
        int result = channelGroupService.addChannelToGroup(channelGroupVO);
        if(result <= 0){
            throw new BusinessException("通道组新增通道失败!");
        }
        return  ResultObject.successMessage("通道组新增通道成功!");
    }

    /**
     * 删除通道组信息
     * @param groupIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:remove')")
    @Log(title = "删除通道组", businessType = BusinessType.DELETE)
    @DeleteMapping("/delChannelGroup/{groupIds}")
    public AjaxResult deleteChannelGroup(@PathVariable Integer[] groupIds){
        return toAjax(channelGroupService.deleteChannelGroup(groupIds));
    }

    /**
     * 删除通道组通道信息
     * @param groupIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('channel:channelGroup:remove')")
    @Log(title = "通道组管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delChannelToGroup/{groupIds}")
    public AjaxResult delChannelToGroup(@PathVariable Integer[] groupIds){
        return toAjax(channelGroupService.deleteChannelToGroup(groupIds));
    }

//    /**
//     * 通道组状态修改
//     */
//    @PreAuthorize("@ss.hasPermi('channel:channelGroup:edit')")
//    @Log(title = "通道组管理", businessType = BusinessType.UPDATE)
//    @PutMapping("/changeGroupStatus")
//    public AjaxResult changeGroupStatus(@RequestBody ProductGroupVO productGroupVo)
//    {
//        return toAjax(channelGroupService.updateProductGroupStatus(productGroupVo));
//    }
//
//    /**
//     * 产品组下产品状态修改
//     */
//    @PreAuthorize("@ss.hasPermi('product:productGroup:edit')")
//    @Log(title = "产品组管理", businessType = BusinessType.UPDATE)
//    @PutMapping("/changeProductToGroupStatus")
//    public AjaxResult changeProductToGroupStatus(@RequestBody ProductToGroupVO productToGroupVo)
//    {
//        return toAjax(productGroupService.updateProductToGroupStatus(productToGroupVo));
//    }
}
